package com.atguigu.gulimall.ssoclient.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Controller
public class HelloController {
    @Value("${sso.server.url}")
    String ssoServerUrl;


    @GetMapping("/boss")
    public  String employees(Model model, HttpSession session,
                             @RequestParam(value = "token", required = false) String token){
        //如果取出token，说明是有用户的，我们这里假设用户名了。
        if (!StringUtils.isEmpty(token)) {
            session.setAttribute("loginUser", "zhangsan");
        }
        Object loginUser = session.getAttribute("loginUser");
        if(loginUser == null){
            //没登陆，跳转到登陆服务器进行登录
            //
            return "redirect:" + ssoServerUrl + "?redirect_url=http://client2.com:8082/employees";
        }else {
            List<String> emps = new ArrayList<>();
            emps.add("张三");
            emps.add("李四");
            model.addAttribute("emps", emps);
            return "employees";
        }

    }
}
